﻿/*===================================================
* CLR版本  : 4.0.30319.42000 
* 类名称   : UIComponent
* 文 件 名 : UIComponent 
* 类描述   :
* 创建人   : oream
* 创建时间 : 2018/3/5 21:43:11
* 修改人   : 
* 修改时间 :
* 版本： @version 1.0
=====================================================*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Model
{
  public enum EUitype
  {
    Loading,
    Login,
    Home,
    Pop,
    PopTwo
  }


  [ObjectSystem]
  public class UIComponentSystem : ObjectSystem<UIComponent>, IAwake
  {
    public void Awake()
    {
      this.Get().Awake();
    }

  }

  public class UIComponent : Component
  {
    private Dictionary<EUitype, IUiFactory> dicUiTypes = new Dictionary<EUitype, IUiFactory>();
    private Dictionary<EUitype, UI> dicUis = new Dictionary<EUitype, UI>();

    private GameObject root;

    public void Awake()
    {
      this.root = GameObject.Find("Canvas");
      this.Start();
    }

    public void Start()
    {
      Type[] types = DllHelper.GetMonoTypes();
      foreach (var type in types)
      {
        object[] tmpObjs = type.GetCustomAttributes(typeof(UiFactoryAttribute), false);
        if (tmpObjs.Length == 0) continue;
        UiFactoryAttribute tmpUiFactory = tmpObjs[0] as UiFactoryAttribute;
        if (tmpUiFactory == null) continue;
        if (this.dicUiTypes.ContainsKey(tmpUiFactory.uiType))
        {
          Debug.LogError($"UI {tmpUiFactory.uiType.ToString()} 已经被添加了, 请检查!");
          throw new Exception($"UI {tmpUiFactory.uiType.ToString()} Existing!");
        }
        object obj = Activator.CreateInstance(type);
        IUiFactory uiFactory = obj as IUiFactory;
        if (uiFactory == null)
        {
          Debug.LogError($"{type.Name} 没有继承 IUiFactory");
          continue;
        }
        this.dicUiTypes.Add(tmpUiFactory.uiType, uiFactory);
      }
    }

    public void Create(EUitype uitype)
    {
      try
      {
        UI uI = this.dicUiTypes[uitype].Create(this.GetParent<Scene>(), uitype, root);
        this.dicUis.Add(uitype, uI);
        // 挂载至Canvas
        uI.gameObject.transform.SetParent(this.root.transform, false);
      }
      catch (Exception e)
      {
        Debug.LogError($"{uitype.ToString()} ui 错误 : {e}");
        throw;
      }
    }

    public void Add(EUitype type, UI ui)
    {
      this.dicUis.Add(type, ui);
    }

    public void Remove(EUitype type)
    {
      UI ui;
      if (!dicUis.TryGetValue(type, out ui))
      {
        return;
      }
      this.dicUiTypes[type].Remove(type);
      dicUis.Remove(type);
      ui.Dispose();
    }

    public void RemoveAll()
    {
      foreach (EUitype type in this.dicUis.Keys.ToArray())
      {
        UI ui;
        if (!this.dicUis.TryGetValue(type, out ui))
        {
          continue;
        }
        this.dicUiTypes[type].Remove(type);
        this.dicUis.Remove(type);
        ui.Dispose();
      }
    }

    public UI Get(EUitype type)
    {
      UI ui;
      this.dicUis.TryGetValue(type, out ui);
      return ui;
    }

    public List<EUitype> GetUITypeList()
    {
      return new List<EUitype>(this.dicUis.Keys);
    }


  }
}
